package api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"main/app"
	"main/internal/model"
	"main/internal/pkg/code"
	"main/internal/pkg/jwtoken"
	"main/internal/service"
	"net/http"
)

func init() {
	c := &SysAdminApi{}
	c.init(app.R) //这里需要引入你的gin框架的实例
}

// SysAdminApi 控制器
type SysAdminApi struct {
	Page     int    `form:"page,default=1"`
	Size     int    `form:"size,default=10"`
	Kw       string `form:"kw"`
	IDS      []uint `form:"ids"`
	ID       uint   `form:"id"`
	DateForm string `form:"date_form"`
	DateTo   string `form:"date_to"`
	Status   string `form:"status"`
	Lang     string `form:"lang"`
}

func (s SysAdminApi) init(g *gin.Engine) {
	// 依次: 分页列表，单条，插入，修改，删除
	group := g.Group("/api/Admin")
	//新增
	group.POST("/Create", s.create)
	//登录
	group.POST("/Login", s.login)
	//批量新增
	group.POST("/CreateList", s.createList)
	//根据ID删除
	group.DELETE("/Delete", s.delete)
	//根据ID批量删除
	group.DELETE("/DeleteList", s.deleteList)
	//根据ID修改
	group.PUT("/Update", s.update)
	//根据ID查询
	group.GET("/Get", jwtoken.JwtAuth(), s.get)
	//批量查询 分页、关键字、ID列表、时间、状态
	group.GET("/GetList", s.getList)
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Admin/Create", ApiMethod: "POST", ApiDescribe: "新增", ApiGroup: "Admin"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Admin/Login", ApiMethod: "POST", ApiDescribe: "登录", ApiGroup: "Admin"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Admin/CreateList", ApiMethod: "POST", ApiDescribe: "批量新增", ApiGroup: "Admin"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Admin/Delete", ApiMethod: "DELETE", ApiDescribe: "根据ID删除", ApiGroup: "Admin"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Admin/DeleteList", ApiMethod: "DELETE", ApiDescribe: "根据ID批量删除", ApiGroup: "Admin"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Admin/Update", ApiMethod: "PUT", ApiDescribe: "根据ID修改", ApiGroup: "Admin"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Admin/Get", ApiMethod: "GET", ApiDescribe: "根据ID查询", ApiGroup: "Admin"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Admin/GetList", ApiMethod: "GET", ApiDescribe: "批量查询", ApiGroup: "Admin"})
}

// create 新增
func (s SysAdminApi) create(c *gin.Context) {
	var v model.SysAdmin
	_ = c.ShouldBindQuery(&s)
	_ = c.ShouldBindJSON(&v)
	uid := c.GetUint("user_id")
	create, err := service.SysAdminService.Create(v, uid)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBInsertError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(create, code.GetMsg(code.DBInsertSuccess, s.Lang)))
	}
}

// login 用户登录
func (s SysAdminApi) login(c *gin.Context) {
	var v model.SysAdmin
	_ = c.ShouldBindQuery(&s)
	_ = c.ShouldBindJSON(&v)
	token, err := service.SysAdminService.Login(v.AdminEmail, v.AdminPassword, s.Lang)
	if err != nil || token == "" {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.LoginFail, s.Lang)+" "+err.Error()))
	} else {
		c.Header("Authorization", token)
		c.JSON(http.StatusOK, model.OkData(token, code.GetMsg(code.LoginSuccess, s.Lang)))
	}
}

// createList 批量新增
func (s SysAdminApi) createList(c *gin.Context) {
	var v []model.SysAdmin
	_ = c.ShouldBindQuery(&s)
	_ = c.ShouldBindJSON(&v)
	uid := c.GetUint("user_id")
	create, err := service.SysAdminService.CreateList(v, uid)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBInsertError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(create, code.GetMsg(code.DBInsertSuccess, s.Lang)))
	}
}

// delete 删除
func (s SysAdminApi) delete(c *gin.Context) {
	_ = c.ShouldBindQuery(&s)
	uid := c.GetUint("user_id")
	deleteDate, err := service.SysAdminService.Delete(s.ID, uid)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBDeleteError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(deleteDate, code.GetMsg(code.DBDeleteSuccess, s.Lang)))
	}
}

// deleteList 根据ID批量删除
func (s SysAdminApi) deleteList(c *gin.Context) {
	_ = c.ShouldBindQuery(&s)
	uid := c.GetUint("user_id")
	deleteDate, err := service.SysAdminService.DeleteList(s.IDS, uid)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBDeleteError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(deleteDate, code.GetMsg(code.DBDeleteSuccess, s.Lang)))
	}
}

// get 根据ID查询
func (s SysAdminApi) get(c *gin.Context) {
	//jwt, _ := jwtoken.ParseJwt(c)
	_ = c.ShouldBindQuery(&s)
	getDate, err := service.SysAdminService.Get(s.ID, s.Lang)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBQueryError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(getDate, code.GetMsg(code.DBQuerySuccess, s.Lang)))
	}
}

// getList 批量查询
func (s SysAdminApi) getList(c *gin.Context) {
	_ = c.ShouldBindQuery(&s)
	fmt.Println(s.Page, "s.Page", s.Size, "s.Size", s.Kw, "s.Kw", s.IDS, "s.IDS", s.ID, "s.ID", s.DateForm, "s.DateForm", s.DateTo, "s.DateTo", s.Status, "s.Status", s.Lang)
	getDate, err, count := service.SysAdminService.GetList(s.Page, s.Size, s.Kw, s.IDS, s.ID, s.DateForm, s.DateTo, s.Status, s.Lang)
	if count == 0 {
		c.JSON(http.StatusOK, model.OkDataList(getDate, code.GetMsg(code.DBQueryError, s.Lang)+" "+err.Error(), count))
		return
	}
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBQueryError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkDataList(getDate, code.GetMsg(code.DBQuerySuccess, s.Lang), count))
	}
}

// update 根据ID更新数据
func (s SysAdminApi) update(c *gin.Context) {
	var v model.SysAdmin
	_ = c.ShouldBindQuery(&s)
	_ = c.ShouldBindJSON(&v)
	uid := c.GetUint("user_id")
	getDate, err := service.SysAdminService.UpdateData(v, uid)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBQueryError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(getDate, code.GetMsg(code.DBQuerySuccess, s.Lang)))
	}
}
